import { isTypeOf } from "../types/helpers";
import { Records } from "../types/types";


/**
 * 序列化查询参数
 * @param params 查询参数
 * @returns 序列化后的查询字符串
 */
export function stringifyParams(params: Records): string {
    if (!isTypeOf(params, 'object')) return '';
    const entries = Object.entries(params)
        .filter(([_, value]) => value !== undefined && value !== null)
        .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);

    return entries.length ? `?${entries.join('&')}` : '';
};